package com.dev;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.provider.MediaStore;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.util.ArrayList;
public class FileBrowserTabMenu extends LinearLayout implements OnClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	FileBrowserTab destination;
	int borderWidth=2;
	Item i0,i1,i2,i3,i4,i5,i6,i7,i8;
	LayoutParams itlp;
	ShowTabDetails std;
	MainActivity ma;
    FileBrowserTabMenu(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new BorderDrawable());
		wlp.gravity=Gravity.LEFT|Gravity.TOP;
		wlp.type=wlp.TYPE_APPLICATION;
		wlp.format=PixelFormat.TRANSPARENT;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		i0=new Item(ma,"克隆标签页");
		i1=new Item(ma,"标签页详情");
		i2=new Item(ma,"重命名");
		i3=new Item(ma,"刷新");
		i4=new Item(ma,"剪贴板");
		i5=new Item(ma,"切换到此目录");
		i6=new Item(ma,"在新标签页中打开");
		i7=new Item(ma,"刷新");
        i8=new Item(ma,"快捷键");
		i0.setOnClickListener(this);
		i1.setOnClickListener(this);
		i2.setOnClickListener(this);
		i3.setOnClickListener(this);
		i4.setOnClickListener(this);
		i5.setOnClickListener(this);
		i6.setOnClickListener(this);
		i7.setOnClickListener(this);
        i8.setOnClickListener(this);
		i4.setVisibility(View.GONE);
		itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
		addView(i0,itlp);
		addView(i1,itlp);
		//addView(i2,itlp);
		//addView(i3,itlp);
		addView(i4,itlp);
		addView(i5,itlp);
		addView(i6,itlp);
		addView(i7,itlp);
	}
	void show(FileBrowserTab fbt,View anchor){
		destination=fbt;
		FileBrowserPage fbp=fbt.fileBrowserPage;
		i6.setVisibility(View.GONE);
		if(fbp!=null){
			i1.setEnabled(true);
			i3.setEnabled(true);
			i5.setEnabled(true);
			i7.setEnabled(true);
			if(fbp.dyn==10||fbp.dyn==11){
				i0.setVisibility(GONE);
				i5.setVisibility(GONE);
				if(fbp.dlv.getVisibility()==View.VISIBLE){
					i7.setVisibility(VISIBLE);
				}else{
					i7.setVisibility(GONE);
				}
			}else if(fbp.dyn==20){//view media images
				i0.setVisibility(GONE);
				if(fbp.dlv.getVisibility()==VISIBLE){
					i7.setVisibility(VISIBLE);
				}else{
					i7.setVisibility(GONE);
				}
			}else{
				i0.setVisibility(VISIBLE);
				i7.setVisibility(GONE);
				if(fbp.dlv.getVisibility()==VISIBLE){
					i5.setVisibility(VISIBLE);
				}else{
					i5.setVisibility(GONE);
				}
			}
			if(fbp.ie!=null&&fbp.ie.getVisibility()==View.VISIBLE){
				i0.setVisibility(GONE);
			}
		}else{
			i1.setEnabled(false);
			i3.setEnabled(false);
			i5.setEnabled(false);
			i7.setEnabled(false);
		}
		Rect rect=new Rect();
		anchor.getGlobalVisibleRect(rect);
		wlp.width=ma.gui.getWidth()*2/3;
		wlp.height=-2;
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		ma.wm.addView(this,wlp);
	}
    void dismiss(){
		ma.wm.removeView(this);
	}
	Point touchDown=new Point();
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int x=(int)event.getX();
		int y=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				touchDown.x=x;
				touchDown.y=y;
				break;
			case MotionEvent.ACTION_UP:
				Rect visible=new Rect();
				getGlobalVisibleRect(visible);
				int[] is=new int[2];
				getLocationOnScreen(is);
				if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
					dismiss();
				}
		}
		super.onTouchEvent(event);
		return true;
	}
	@Override
	public void onClick(View v){
		if(v==i0){
			FileBrowserTab fbt=new FileBrowserTab(ma);
			TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
			fbi.home=destination.fbi.home;
			fbi.currentDir=destination.fbi.currentDir;
			fbt.fbi=fbi;
			fbt.setText(fbi.home.getName());
			fbt.setOnClickListener(ma.gui.TabOnClick);
			for(int index=0,count=ma.gui.tabs.getChildCount();true;index++){
				if(index<count){
					View child=ma.gui.tabs.getChildAt(index);
					if(child==destination){
						ma.gui.tabs.addView(fbt,index+1,TabBar.tablp);
						ma.gui.TabOnClick.onClick(fbt);
						ma.gui.saveAllTab();
						break;
					}
				}else{
					break;
				}
			}
			dismiss();
		}else if(v==i1){//onClick
			dismiss();
			FileBrowserPage fbp=destination.fileBrowserPage;
			if(fbp.dyn==10||fbp.dyn==11){
				class MediaAudioDetails extends LinearLayout implements OnClickListener{
					WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
					LinearLayout titlebar;
					IconViewClose close;
					TextView title;
					TextView msg;
					File destination;
					MainActivity ma;
					MediaAudioDetails(MainActivity ma){
						super(ma);
						this.ma=ma;
						setOrientation(VERTICAL);
						setWillNotDraw(false);
						title=new TextView(ma);
						title.setTextColor(0xff000000);
						title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
						title.setGravity(Gravity.CENTER_VERTICAL);
						title.setPadding(9,0,0,0);
						title.setText("媒体音乐");
						close=new IconViewClose(ma);
						close.setOnClickListener(this);
						titlebar=new LinearLayout(ma){
							public void onDraw(Canvas cvs){
								super.onDraw(cvs);
								Paint p=new Paint();
								p.setColor(FileBrowserPage.borderColor);
								cvs.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
							}
						};
						titlebar.setWillNotDraw(false);
						titlebar.setOrientation(HORIZONTAL);
						titlebar.addView(title,new LayoutParams(0,-1,1));
						titlebar.addView(close);
						msg=new TextView(ma);
						msg.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
						msg.setTypeface(Typeface.MONOSPACE);
						msg.setTextColor(0xff000000);
						msg.setPadding(10,0,0,0);
						msg.setMovementMethod(LinkMovementMethod.getInstance());
						msg.setLinksClickable(true);
						addView(titlebar,-1,(int)(30*ma.dp));
						addView(msg,-1,-2);
					}
					void show(FileBrowserTab fbt){
						wlp.width=ma.wp*3/4;
						wlp.height=-2;
						FileBrowserPage fbp=fbt.fileBrowserPage;
						msg.setText("总有："+fbp.files.length+"项");
						ma.wm.addView(this,wlp);
					}
					@Override 
					public void onClick(View v){
						if(v==close){
							ma.wm.removeView(this);
						}
					}
					@Override
					protected void onDraw(Canvas canvas){
						Paint p=new Paint();
						p.setColor(FileBrowserPage.borderColor);
						p.setStyle(Paint.Style.FILL);
						canvas.drawColor(0xffffffff);
						canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
						canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
						canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
						canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
						super.onDraw(canvas);
					}
				}
				MediaAudioDetails mad=new MediaAudioDetails(ma);
				mad.show(destination);
			}else{
				if(ma.fileDetails==null)ma.fileDetails=new ShowFileDetails(ma);
				ma.fileDetails.show(destination.fbi.home,fbp);
			}
		}else if(v==i5){//onClick
			FileBrowserPage fbp=destination.fileBrowserPage;
			if(fbp.home.equals(fbp.currentDirectory)){
				Toast.makeText(ma,"无须此操作",0).show();
			}else{
				for(int i=0,lim=ma.tabList.ts.size();true;i++){
					if(i<lim){
						Object child=ma.tabList.ts.get(i);
						if(child instanceof TabData.FileBrowserInfo){
							TabData.FileBrowserInfo fbi=(TabData.FileBrowserInfo)child;
							if(fbi==destination.fbi||fbi.home.equals(destination.fbi.home)){
								fbp.home=fbp.currentDirectory;
								destination.fbi.home=fbp.currentDirectory;
								destination.setText(fbp.currentDirectory.getName());
								fbi.home=fbp.currentDirectory;
								ma.tdm.updateSavedTabList(ma.tabList);
							}
						}
					}else{
						break;
					}
				}
				dismiss();
			}
		}else if(v==i7){//onClick
			ArrayList<File> fs=new ArrayList<File>();
			try{
				//MediaStore.
				ContentResolver mContentResolver=ma.getContentResolver();
				Cursor c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
				int columnIndexOrThrow_DATA = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
				while(c.moveToNext()){
					String path = c.getString(columnIndexOrThrow_DATA);
					File f=new File(path);
					fs.add(f);
				}
			}catch(Exception e){
				log.alert(e.toString());
			}
			File[] files=new File[fs.size()];
			fs.toArray(files);
			FileBrowserPage fbp=destination.fileBrowserPage;
			fbp.files=files;
			fbp.setWillShow(files);
			dismiss();
			Toast.makeText(ma,"已刷新",0).show();
		}
	}
	public static class BorderDrawable extends ColorDrawable {
		int borderWidth=2;
		Paint border=new Paint();{
			border.setColor(MainActivity.CYAN);
			border.setStyle(Paint.Style.FILL);
		}
		@Override
		public void draw(Canvas cvs) {
			int w=cvs.getWidth();
			int h=cvs.getHeight();
			cvs.drawColor(0xffffffff);
			cvs.drawRect(0, 0, borderWidth, h, border);
			cvs.drawRect(0, 0, w, borderWidth, border);
			cvs.drawRect(w - borderWidth, 0, w, h, border);
			//cvs.drawRect(0,h-borderStrokeWidth,w,h,border);
		}
	}
	public static  class Item extends TextView {
		int borderWidth=2;
		int textColor=0xff000000;
		public Item(Context ctx, String s) {
			super(ctx);
			setText(s);
			setTypeface(Typeface.MONOSPACE);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
			setTextColor(textColor);
			setGravity(Gravity.CENTER_VERTICAL);
			setPadding(5, 0, 0, 0);
			StateListDrawable sd=new StateListDrawable();
			sd.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(0xff777777));
			setBackgroundDrawable(sd);
		}
		@Override
		public void setEnabled(boolean enabled){
			if(enabled)setTextColor(textColor);else setTextColor(0x77888888);
			super.setEnabled(enabled);
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Paint p=new Paint();
			p.setColor(MainActivity.CYAN);
			canvas.drawRect(0, canvas.getHeight() - borderWidth, canvas.getWidth(), canvas.getHeight(), p);
		}
	}
	public class ShowTabDetails extends LinearLayout implements OnClickListener{
		ShowTabDetails(MainActivity ma){
			super(ma);
		}
		@Override
		public void onClick(View v){
		}
	}
    public static class ShowShortcut extends View{
        WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
        MainActivity ma;
        ShowShortcut(MainActivity ma){
            super(ma);
            this.ma=ma;
        }
        void show(){
            
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
        }
    }
}
